﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class GpsDeviceConfigBLL
    {
        #region 构造单例
        private GpsDeviceConfigBLL() { }
        private static GpsDeviceConfigBLL _instance;
        public static GpsDeviceConfigBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new GpsDeviceConfigBLL());
            }
        }
        #endregion

        public int Add(Gpsdeviceconfig info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Gpsdeviceconfig.Where(p => p.GpsDeviceId == info.GpsDeviceId).ToList();
                if (exist != null && exist.Count>0)
                {
                    return -2;
                }
                ef.Gpsdeviceconfig.Add(info);
                ef.Entry<Gpsdeviceconfig>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public int Update(Gpsdeviceconfig info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Gpsdeviceconfig.Where(p => p.ConfigId == info.ConfigId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Gpsdeviceconfig.Where(p => p.GpsDeviceId == info.GpsDeviceId && p.ConfigId != info.ConfigId).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                }
                exist.GpsDeviceId = info.GpsDeviceId;
                exist.BindId = info.BindId;
                exist.BindType = info.BindType;
                exist.CreateTime = info.CreateTime;
                ef.Gpsdeviceconfig.Add(exist);
                ef.Entry<Gpsdeviceconfig>(exist).State = EntityState.Modified;
                return ef.SaveChanges();
            }
        }

        public int Remove(int configId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Gpsdeviceconfig.Where(p => p.ConfigId == configId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                ef.Gpsdeviceconfig.Add(exist);
                ef.Entry<Gpsdeviceconfig>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();
            }
        }

        public List<GpsDeviceConfigModel> GetByPage(int? bindType,RequestPageBase page)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select b.ConfigID,a.*,b.BindType,b.BindID, ";
                sql += " (case b.BindType when 0 then d.RealName when 1 then c.CarName end)as BindName,  ";
                sql += " (if (b.BindType is null,0,1))as IsBind ";
                sql += " from gpsdevice as a  ";
                sql += " left join gpsdeviceconfig as b on a.GpsDeviceID = b.GpsDeviceID  ";
                sql += " left join carinfo as c on b.BindID=c.CarID ";
                sql += " left join userinfo as d on b.BindID=d.UserID ";
                sql += " where 1=1  {0} ";
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += $"  a.GpsDeviceNo like '%{page.Keyword}%'";
                    paramsSql += $" or a.GpsDeviceName like '%{page.Keyword}%'";
                    paramsSql += $" or d.RealName like '%{page.Keyword}%'";
                    paramsSql += $" or c.CarName like '%{page.Keyword}%'";
                    paramsSql = $" and ({paramsSql})";
                }
                if(bindType.HasValue)
                {
                    paramsSql += $" and b.BindType='{bindType.Value}'";
                }
                paramsSql += page.TimeSql(" a.CreateTime ");
                sql = string.Format(sql, paramsSql);
                var countSql = sql.CountSql();
                page.TotalRows = Convert.ToInt32(ef.Database.SqlQuery(countSql).Rows[0][0]);
                sql += page.OrderByPattern == 0 ? "a.CreateTime".OrderByDescending() : "a.CreateTime".OrderBy();
                sql += page.Limit();
                var list = ef.Database.SqlQuery<GpsDeviceConfigModel>(sql);
                return list;
            }
        }

        public dynamic Get(int bindType, int bindId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                try
                {
                    var infos = ef.Gpsdeviceconfig.Where(p => p.BindType == bindType && p.BindId == bindId).ToList();
                    var result = (from a in infos
                                join b in ef.Gpsdevice on a.GpsDeviceId equals b.GpsDeviceId
                                into c
                                let d = c.FirstOrDefault()
                                select new
                                {
                                    a.ConfigId,
                                    a.GpsDeviceId,
                                    a.BindType,
                                    a.CreateTime,
                                    a.BindId,
                                    GpsDeviceName = d.GpsDeviceName,
                                    GpsDeviceNo = d.GpsDeviceNo,
                                }).FirstOrDefault();
                    return result;
                }
                catch (Exception ex) {
                    return null;
                }

            }
        }

        public GpsDeviceConfigModel Get(string gpsDeviceNo)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.GpsDeviceNo from gpsdeviceconfig as a  ";
                sql+= "left join gpsdevice as b on a.GpsDeviceID = b.GpsDeviceID  where 1=1";
                if (!string.IsNullOrEmpty(gpsDeviceNo))
                {
                    sql += $" and b.GpsDeviceNo='{gpsDeviceNo}'";
                }
                var list = ef.Database.SqlQuery<GpsDeviceConfigModel>(sql);
                return list?.FirstOrDefault();
            }
        }

    }
}
